package com.xhwl.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.dto.cent.sdata.devplatform.DeveloperDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.App;
import com.xhwl.common.pojo.cent.sdata.devplatform.Developer;
import com.xhwl.common.query.cent.sdata.devplatform.DeveloperQuery;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.data.pojo.vo.DevelopRequestVO;
import com.xhwl.data.service.IAccountService;
import com.xhwl.data.service.IFileUploadRecordService;
import com.xhwl.data.service.IMessageCodeService;
import com.xhwl.data.service.IRegionService;
import com.xhwl.data.service.devplatform.IDeveloperService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 15:06 2022/6/21
 */
@RestController
public class DeveloperController {

    @Autowired
    private IDeveloperService developerService;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IMessageCodeService messageCodeService;

    private static final Logger log = LoggerFactory.getLogger(DeveloperController.class);

    /**
     * 发起开发者注册时，判断是否存在审核中的租户名称及社会统一信用代码
     *
     * @return
     */
    @GetMapping(value = "/developer/check")
    public ResultJson checkDeveloper(@RequestParam(value = "code") String code,
                                     @RequestParam(value = "creditCode", required = false) String creditCode) {
        return developerService.checkDeveloper(code, creditCode);
    }

    /**
     * 校验是否存在审核中的企业名称和信用代码
     */
    @GetMapping(value = "/developer/check-enterprise")
    public ResultJson checkEnterprise(@RequestParam(value = "creditCode",required = true) String creditCode) {
        return developerService.checkEnterprise(creditCode);
    }
    /**
     * 注册开发者
     *
     * @return
     */
    @PostMapping("/developer/register")
    public ResultJson registerDeveloper(@RequestHeader(required = false) Integer industryId,
                                        @RequestBody @Validated DeveloperDTO developer, BindingResult bindingResult) {

        ResultJson resultJson = messageCodeService.phoneMsgCodeVerify(String.valueOf(industryId), developer.getAccountDTO().getPhone(), developer.getVerifyCode(), true);
        if (!resultJson.getState()) {
            return resultJson;
        }
        developer.setAccountIndustryId(industryId);
        developer.getAccountDTO().setIndustryId(industryId.shortValue());
        return developerService.registerDeveloper(developer);
    }

    /**
     * 更新企业信息
     *
     * @param developer
     * @return
     */
    @PostMapping("/developer/update-ent")
    public ResultJson update(@RequestBody @Validated Developer developer, BindingResult bindingResult) {
        if (null == developer.getId()) {
            return ResultJson.fail("缺少id");
        }
        Developer developer1 = developerService.getById(developer.getId());
        if (null == developer1) {
            return ResultJson.fail("开发者不存在");
        }
        developer1.setCorporator(developer.getCorporator());
        developer1.setRProvinceId(developer.getRProvinceId());
        developer1.setRCityId(developer.getRCityId());
        developer1.setRDistrictId(developer.getRDistrictId());
        developer1.setRDetail(developer.getRDetail());
        developer1.setWProvinceId(developer.getWProvinceId());
        developer1.setWCityId(developer.getWCityId());
        developer1.setWDistrictId(developer.getWDistrictId());
        developer1.setWDetail(developer.getWDetail());

        developer1.setContacts(developer.getContacts());
        developer1.setContactsPhone(developer.getContactsPhone());
        developer1.setBusinessLicense(developer.getBusinessLicense());
        developerService.updateById(developer1);
        return ResultJson.success();
    }


    /**
     * 开发者审核
     *
     * @return
     */
    @PostMapping("/developer/audit")
    public ResultJson auditDeveloper(@RequestBody DeveloperDTO developerDTO, App app) {
        return developerService.auditDeveloper(developerDTO, app.getIndustryId().intValue());
    }

    /**
     * 详情
     *
     * @return
     */
    @PostMapping("/developer/detail")
    public ResultJson detail(@RequestBody DeveloperQuery developerQuery, Developer developer) {
        DeveloperDTO developerDTO = developerService.detail(null == developerQuery.getId() ? developer.getId() : developerQuery.getId());
        String r = regionService.getRegion(developerDTO.getRProvinceId(), developerDTO.getRCityId(), developerDTO.getRDistrictId());
        String w = regionService.getRegion(developerDTO.getWProvinceId(), developerDTO.getWCityId(), developerDTO.getWDistrictId());
        developerDTO.setRAddress(r.trim().length() > 0 ? r + developerDTO.getRDetail() : developerDTO.getRDetail());
        developerDTO.setWAddress(w.trim().length() > 0 ? w + developerDTO.getWDetail() : developerDTO.getWDetail());
        // 回填主账号数据
        if (null != developerDTO.getAccountId()) {
            Account account = accountService.findById(developerDTO.getAccountId());
            account.setPassword("");
            AccountDTO accountDTO = new AccountDTO();
            BeanUtils.copyProperties(account, accountDTO);
            developerDTO.setAccountDTO(accountDTO);
        } else {
            log.warn("开发者 {} 缺少主账号信息", developerDTO.getId());
        }
        return ResultJson.success(developerDTO);
    }

    /**
     * 分页查询
     *
     * @param developRequestVO
     * @return
     */
    @PostMapping("/developer/page")
    public ResultJson page(@RequestBody DevelopRequestVO developRequestVO) {
        return ResultJson.success(developerService.page(developRequestVO.getDeveloperQuery(), developRequestVO.getPage()));
    }

    /**
     * 更新开发者状态
     *
     * @param developerDTO
     * @return
     */
    @PostMapping("/develop/update-status")
    public ResultJson updateStatus(@RequestBody DeveloperDTO developerDTO) {
        Developer developer = developerService.getById(developerDTO.getId());
        if (null == developer) {
            return ResultJson.fail("缺少开发者信息");
        }
        developer.setStatus(developerDTO.getStatus());
        developerService.updateById(developer);
        return ResultJson.success();
    }

}
